import tkinter as tk
# from saes1 import encrypt,decrypt,get_Key#一重加密界面
from saes2 import encrypt, decrypt, get_Key  # 二重加密界面
# from saes3 import encrypt,decrypt,get_Key#三重加密界面
# from saesASCII import encrypt,decrypt,get_Key#ascii界面
from tkinter import messagebox

# 界面设计
top = tk.Tk()
top.title("S-AES encryption/decryption program")
top.geometry('700x700')


# 获取输入
# def wrong():
#     print('wrong')
#     messagebox.showinfo('提示', '请检查输入：\n（1）明文/密文是否为8bit二进制或单个ASCII字符；\n（2）密钥是否为10bit二进制。')


def getEncrypt():
    t.configure(state=tk.NORMAL)
    p = entry.get()
    # if p.isdigit():
    #     print("明文为数字")
    # else:
    #     p = '{:0>8}'.format(str(bin(ord(p))[2:]))
    #     print("明文为字母，已转换为数字")
    key = entry1.get()
    # if len(p) == 8 and all(x in "01" for x in p) and len(key) == 10 and all(x in "01" for x in key):
    print("plaintext:" + p + "\nkey:" + key)
    get_Key(key)
    ciphertext = encrypt(p)  # 加密
    print("--加密-->\nciphertext:" + ciphertext)
    t.delete('1.0', 'end')
    t.insert('1.0', ciphertext)
    t.configure(state=tk.DISABLED)
    # else:
    #     wrong()


def getDecrtpt():
    t.configure(state=tk.NORMAL)
    p = entry.get()
    # if p.isdigit():
    #     print("明文为数字")
    # else:
    #     p = '{:0>8}'.format(str(bin(ord(p))[2:]))
    #     print("明文为字母，已转换为数字")
    key = entry1.get()
    # if len(p) == 8 and all(x in "01" for x in p) and len(key) == 10 and all(x in "01" for x in key):
    print("ciphertext:" + p + "\nkey:" + key)
    get_Key(key)
    ciphertext = decrypt(p)  # 加密
    print("--解密-->\nplaintext:" + ciphertext)
    t.delete('1.0', 'end')
    t.insert('1.0', ciphertext)
    t.configure(state=tk.DISABLED)
    # else:
    #     wrong()


# 输入框
tk.Label(top, text="Plaintext/Ciphertext：", font=("华文新魏", 18)).place(x=60, y=40)
entry = tk.Entry(top)
entry.place(x=60, y=100, width=550, height=60)
entry.configure(font=("华文新魏", 16))

tk.Label(top, text="Key：", font=("华文新魏", 18)).place(x=60, y=180)
entry1 = tk.Entry(top)
entry1.place(x=60, y=220, width=550, height=60)
entry1.configure(font=("华文新魏", 16))

tk.Label(top, text="Result：", font=("华文新魏", 18)).place(x=60, y=320)
t = tk.Text(top, width=45, height=3)
t.place(x=60, y=400)
t.configure(font=("华文新魏", 16))

# button
tk.Button(top, text=" encrypt ", borderwidth=0, bg="red", activeforeground="#B0C4DE", font=("华文新魏", 18),
          command=getEncrypt).place(x=140, y=550)
tk.Button(top, text=" decrypt ", borderwidth=0, bg="red", activeforeground="#B0C4DE", font=("华文新魏", 18),
          command=getDecrtpt).place(x=460, y=550)

top.mainloop()
